package com.openxt.nbzj.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.R;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.RemotingEx;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.progress.Wait;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-30.
 */
public class ProjectDetailsActivity extends OpenxtBaseActivity implements RemotingEx.OnActionListener {
    @BindView(R.id.PUBLISHDATE)
    TextView PUBLISHDATE;
    @BindView(R.id.NAME)
    TextView NAME;
    @BindView(R.id.PROFILE)
    TextView PROFILE;
    @BindView(R.id.CAIE)
    TextView CAIE;
    @BindView(R.id.MANAGER)
    TextView MANAGER;

    Handler mHandler = new Handler();
    Context mContext = null;
    RemotingModel remotingModel = null;
    String ID = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setBaseStyle(R.layout.project_detail_activity, "项目信息详情");
        ButterKnife.bind(this);
        setActionUrl("#/project");
        mContext = this;
        remotingModel = new RemotingModel(mContext);

        Bundle bundle = getIntent().getExtras();
        if(bundle == null)
            return;
        ID = bundle.getString("ID");

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initDetail();
            }
        }, 100);
    }

    private void initDetail(){
        String val = Utils.getLocalStorage(mContext, ConfigModel.PROJECT_GETINFO + "$" + ID);
        if(!StringUtil.isNull(val)){
            initDetailView(val);
            remotingModel.doProjectGetinfo(this, false, ID);
        }else{
            Wait.sharedInstance().start(mContext, "正在获取详情..., 请稍候");
            remotingModel.doProjectGetinfo(this, false, ID);
        }
    }

    private void initDetailView(String val){
        if(StringUtil.isNull(val))
            return;
        try{
            JSONObject info = JSON.parseObject(val);
            String $NAME = info.getString("NAME");
            if(StringUtil.isNull($NAME))
                $NAME = "无";
            NAME.setText("\u3000\u3000" + $NAME);

            String $PROFILE = info.getString("PROFILE");
            if(StringUtil.isNull($PROFILE))
                $PROFILE = "无";
            PROFILE.setText($PROFILE);

            String $PUBLISHDATE = info.getString("PUBLISHDATE");
            if(StringUtil.isNull($PUBLISHDATE))
                $PUBLISHDATE = "无";
            else
                $PUBLISHDATE = $PUBLISHDATE.substring(0, 10);
            PUBLISHDATE.setText($PUBLISHDATE);

            String $CAIE = info.getString("CAIE");
            if(StringUtil.isNull($CAIE))
                $CAIE = "无";
            CAIE.setText($CAIE);

            String $MANAGER = info.getString("MANAGER");
            if(StringUtil.isNull($MANAGER))
                $MANAGER = "无";
            MANAGER.setText($MANAGER);
        }catch (Exception e){

        }
    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        Wait.sharedInstance().stop();
        if(actionName.equals(ConfigModel.PROJECT_GETINFO + "$" + ID)){
            Utils.setLocalStorage(mContext, ConfigModel.PROJECT_GETINFO + "$" + ID, reVal);
            initDetailView(reVal);
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
        Wait.sharedInstance().stop();
        showMsg(errorVal);
    }
}
